package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.*;
import com.hxgz.steel.service.BackendOrderService;
import com.hxgz.steel.service.ConsumerCenterService;
import com.hxgz.steel.service.QuotationManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "ConsumerCenterController", description = "前台:买家中心")
@RestController
@RequestMapping("consumerCenter")
public class ConsumerCenterController {

  @Autowired
  ConsumerCenterService consumerCenterService;

  @Autowired
  QuotationManageService quotationManageService;

  @Autowired
  BackendOrderService backendOrderService;


  @ApiOperation("实单求购查询")
  @RequestMapping(value = "checkAskBuyList.do", method = RequestMethod.POST)
  public ServerResponse<PageInfo<List<AskBuyDto>>> check(
      @ApiParam(value = "'状态  0正在报价  1报价已结束  2已取消") @RequestParam(required = false) Integer status,
      Page page, @ApiParam(value = "开始日期") @RequestParam(required = false) String startDate,
      @ApiParam(value = "结束日期") @RequestParam(required = false) String endDate,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.check(user, page, startDate, endDate, status);
  }

  @ApiOperation("微信实单求购查询")
  @RequestMapping(value = "WxCheckAskBuyList.do", method = RequestMethod.POST)
  public ServerResponse<PageInfo<List<AskBuyDto>>> WxCheckAskBuyList(
          @ApiParam(value = "'状态  0正在报价  1报价已结束  2已取消") @RequestParam(required = false) Integer status,
          Page page, @ApiParam(value = "开始日期") @RequestParam(required = false) String startDate,
          @ApiParam(value = "结束日期") @RequestParam(required = false) String endDate,
          HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.WxOperatedBuy(user, page, startDate, endDate, status);
  }


  @ApiOperation("指定商家求购查询")
  @RequestMapping(value = "appointAskBuy.do", method = RequestMethod.POST)
  public ServerResponse<PageInfo<AppointAskBuyDto>> appointAskBuy(
      @ApiParam("商家求购状态 0卖家为报价 1卖家已报价 2卖家已拒绝 3已取消") @RequestParam(required = false) Integer status,
      @ApiParam(value = "开始日期") @RequestParam(required = false) String startDate,
      @ApiParam(value = "结束日期") @RequestParam(required = false) String endDate, Page page,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.appointAskBuyList(user, status, startDate, endDate, page);
  }

  @ApiOperation("微信指定商家求购查询")
  @RequestMapping(value = "WxAppointAskBuy.do", method = RequestMethod.POST)
  public ServerResponse<PageInfo<AppointAskBuyDto>> WxAppointAskBuy(
          @ApiParam("商家求购状态 0卖家为报价 1卖家已报价 2卖家已拒绝 3已取消") @RequestParam(required = false) Integer status,
          @ApiParam(value = "开始日期") @RequestParam(required = false) String startDate,
          @ApiParam(value = "结束日期") @RequestParam(required = false) String endDate, Page page,
          HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.WxAppointAskBuyList(user, status, startDate, endDate, page);
  }


  @ApiOperation("订单求购详情")
  @RequestMapping(value = "checkAskBuyListDetail.do", method = RequestMethod.POST)
  public ServerResponse<AskBuyDetailDto> checkAskBuyById(@RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.checkAskBuyById(id);
  }


  @ApiOperation("取消求购")
  @RequestMapping(value = "cancleQuotation.do", method = RequestMethod.POST)
  public ServerResponse cancleQuotation(@RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.cancleQuotation(id);
  }


  @ApiOperation("下载清单")
  @RequestMapping(value = "downExcel.do", method = RequestMethod.GET)
  public void downExcel(@ApiParam("报价id") @RequestParam String id,
      HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return;
//      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
//          ResponseCode.NEED_LOGIN.getDesc());
    }
    quotationManageService
        .downloadExcel(Long.valueOf(id), user.getId(), httpServletResponse,httpServletRequest);
  }


  @ApiOperation("我的订单查询")
  @RequestMapping(value = "checkOrderList.do", method = RequestMethod.POST)
  public ServerResponse<PageInfo<List<OrderDto>>> checkOrder(
      @ApiParam("第三方卖家订单 0 风水网自营订单1") @RequestParam Integer index,
      @ApiParam("订单状态  0待付款 1 待发货 2已发货 3已完成 4已取消 5已关闭 6已删除") @RequestParam(required = false) Integer status,
      Page page, @ApiParam("开始日期") @RequestParam(required = false) String startDate,
      @ApiParam("结束日期") @RequestParam(required = false) String endDate,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.checkOrder(index, status, page, startDate, endDate, user);
  }


  @ApiOperation("订单明细查询[新接口]")
  @RequestMapping(value = "checkOrderDetail.do", method = RequestMethod.POST)
  public ServerResponse<OrderDetailDto> checkOrderDetail(@ApiParam("订单id") @RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.checkOrderDetail(id);
  }

  @ApiOperation("订单明细删除")
  @RequestMapping(value = "delOrderDetail.do", method = RequestMethod.POST)
  public ServerResponse delOrderDetail(@ApiParam("订单id") @RequestParam  String id,
                                         HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.delOrderDetail(id);
  }

  @ApiOperation("获得用户所有地址")
  @RequestMapping(value = "getAddressList.do", method = RequestMethod.POST)
  public ServerResponse getAddressList(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    Long userId = user.getId();
    return consumerCenterService.getAddressList(userId);
  }

  @ApiOperation("获得用户默认地址")
  @RequestMapping(value = "getAddress.do", method = RequestMethod.POST)
  public ServerResponse getAddress(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    Long userId = user.getId();
    return consumerCenterService.getAddress(userId);
  }

  @ApiOperation("设置用户默认地址")
  @RequestMapping(value = "setAddress.do", method = RequestMethod.POST)
  public ServerResponse setAddress(@ApiParam("地址id") @RequestParam  Long id,
          HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    Long userId = user.getId();
    return consumerCenterService.setAddress(userId,id);
  }

  @ApiOperation("取消订单")
  @RequestMapping(value = "cancelOrder.do",method = RequestMethod.POST)
  public ServerResponse cancelOrder(Long id, HttpServletRequest httpServletRequest){
    User user = Const.checkUserLogin(httpServletRequest);
    if(user == null){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.updateOrderStatus(id,4,user.getId());
  }

  @ApiOperation("确认订单")
  @RequestMapping(value = "confirmOrder.do",method = RequestMethod.POST)
  public ServerResponse confirmOrder(Long id, HttpServletRequest httpServletRequest){
    User user = Const.checkUserLogin(httpServletRequest);
    if(user == null){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.updateOrderStatus(id,3,user.getId());
  }

  @ApiOperation("删除订单")
  @RequestMapping(value = "delOrder.do",method = RequestMethod.POST)
  public ServerResponse delOrder(Long id, HttpServletRequest httpServletRequest){
    User user = Const.checkUserLogin(httpServletRequest);
    if(user == null){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.updateOrderStatus(id,6,user.getId());
  }

  @ApiOperation("买家中心采购总览")
  @RequestMapping(value = "buyList.do", method = RequestMethod.POST)
  public ServerResponse buyList(HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.buyList(user);
  }

  @ApiOperation("商品排名")
  @RequestMapping(value = "merchantRankList.do", method = RequestMethod.POST)
  public ServerResponse merchantRankList(HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.merchantRankList();
  }

  @ApiOperation("商品报价购买查看详情")
  @RequestMapping(value = "offerBuy.do", method = RequestMethod.POST)
  public ServerResponse offerBuy(@ApiParam("报价id") @RequestParam(required = false) String quId,
      @ApiParam("求购id") @RequestParam(required = false) String asId,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.offerBuy(quId, asId);
  }

  /*  该方法弃用*//*
  @ApiOperation("订单生成购买")
  @RequestMapping(value = "buy.do", method = RequestMethod.POST)
  public ServerResponse buyForCreatOrder(@ApiParam("商家id") String merchantId,
      @ApiParam("实单求购id") String quotationId,
      @ApiParam("地址id") String addressId,
      @ApiParam("交易金额") String money, @ApiParam("是否开发票 0不开发票 1开发票") Integer invoice,
      @ApiParam("备注") String remark, HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService
        .buyForCreatOrder(merchantId, quotationId, addressId, money, invoice, remark,user);
  }*/

  @ApiOperation("删除求购")
  @RequestMapping(value = "delQua.do", method = RequestMethod.POST)
  public ServerResponse deleteQua(@ApiParam("求购id")@RequestParam Long id,HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.deletQuotation(id);
  }

  @ApiOperation("重新发起求购")
  @RequestMapping(value = "repeatQuotation.do", method = RequestMethod.POST)
  public ServerResponse repeatQuotation(Long askId,  HttpServletRequest httpServletRequest) {
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.repeatQuotation(askId, user);
  }

  @ApiOperation("（小程序）个人中心-自营订单")
  @RequestMapping(value="WXselforder.do",method =RequestMethod.POST )
  public ServerResponse<PageInfo<List<WXorderDto>>> WXselforder(HttpServletRequest httpServletRequest, Page page,
                                                                @RequestParam(required = false) @ApiParam("订单状态  0待付款 1 待发货 3已完成 4已取消") Integer status){
    User user = Const.checkUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.WXselforder(status,page,user.getId());
  }

//  @ApiOperation("（小程序）个人中心-第三方订单")
//  @RequestMapping(value="WXthirdorder.do",method =RequestMethod.POST )
//  public ServerResponse<PageInfo<List<WXorderDto>>> WXthirdorder(HttpServletRequest httpServletRequest,Page page,
//                                    @RequestParam(required = false) @ApiParam("订单状态  0待付款 1 待发货 3已完成 4已取消") Integer status){
//    User user = Const.checkUserLogin(httpServletRequest);
//    if (user == null) {
//      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
//              ResponseCode.NEED_LOGIN.getDesc());
//    }
//    return consumerCenterService.WXthirdorder(status,page,user.getId());
//  }


  @ApiOperation("(小程序) 个人中心--自营求购,第三方求购")
  @RequestMapping(value = "WxSelfOperatedBuy.do",method = RequestMethod.POST)
  public ServerResponse WxSelfOperatedBuy(HttpServletRequest request,Page page,@RequestParam(required = false) @ApiParam("订单状态 0正在报价 1报价已结束 2已取消") Integer status,
                                          @RequestParam(required = false)@ApiParam("自营求购 传递1 非自营求购不传") Long merchantId){
    User user = Const.checkUserLogin(request);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
              ResponseCode.NEED_LOGIN.getDesc());
    }
    ServerResponse serverResponse = consumerCenterService.WxSelfOperatedBuy(status,page,user.getId(),merchantId);
    return serverResponse;
  }

  @ApiOperation("(小程序)商城实单求购订单查询")
  @RequestMapping(value = "WxSelfBuyOrder.do", method = RequestMethod.POST)
  public ServerResponse<WxSelfBuyOrderDto> WxSelfBuyOrder(@RequestParam(required = false) @ApiParam("订单状态 0待付款 1 待发货 2已发货 3已完成 4已取消") Integer status, HttpServletRequest request, Page page) {
    User user = Const.checkUserLogin(request);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }
    return consumerCenterService.wxSelfBuyOrder(status,user.getId(),page);
  }

}
